package com.example.himalaya.presenters;

import androidx.annotation.Nullable;

import com.example.himalaya.data.XimalayaApi;
import com.example.himalaya.interfaces.IRecommendPresenter;
import com.example.himalaya.interfaces.IRecommendViewCallBack;
import com.example.himalaya.utils.LogUtil;
import com.ximalaya.ting.android.opensdk.datatrasfer.IDataCallBack;
import com.ximalaya.ting.android.opensdk.model.album.Album;
import com.ximalaya.ting.android.opensdk.model.album.GussLikeAlbumList;

import java.util.ArrayList;
import java.util.List;

/**
 * 推荐业务逻辑接口实现类
 * P接口实现类
 */
public class RecommendPresenter implements IRecommendPresenter {
    private static final String TAG = "RecommendPresenter";

    private List<IRecommendViewCallBack> mRecommendViewCallBacks = new ArrayList<>();//推荐UI对象的缓存集合
    private List<Album> mCurrentRecommendList=null;
    private List<Album> mRecommendAlbumList;

    private RecommendPresenter() {
    }

    private static RecommendPresenter sRecommendPresenter;

    /**
     * 获取推荐逻辑实现类的单例对象
     * @return
     */
    public static RecommendPresenter getRecommendPresenter() {
        if (sRecommendPresenter == null) {
            synchronized (RecommendPresenter.class) {
                if (sRecommendPresenter == null) {
                    sRecommendPresenter = new RecommendPresenter();
                }
            }
        }
        return sRecommendPresenter;
    }

    /**
     * 获取当前推荐专辑列表
     * @return 推荐专辑列表，使用之前要进行判空
     */
    public List<Album> getCurrentRecommendList() {
        return mCurrentRecommendList;
    }

    @Override
    public void getRecommendAlbumList() {
        //获得数据前，页面更新为正在加载页面
        updateLoading();

        if (mRecommendAlbumList != null && mRecommendAlbumList.size() > 0) {
            handlerRecommendResult(mRecommendAlbumList);
            return;
        }

        XimalayaApi ximalayaApi = XimalayaApi.getmXimalayaApi();
        ximalayaApi.getRecommendList(new IDataCallBack<GussLikeAlbumList>() {
            @Override
            public void onSuccess(@Nullable GussLikeAlbumList gussLikeAlbumList) {
                if (gussLikeAlbumList != null) {
                    mRecommendAlbumList = gussLikeAlbumList.getAlbumList();
                    //数据回来后，处理结果，更新页面
                    handlerRecommendResult(mRecommendAlbumList);
                }
            }

            @Override
            public void onError(int i, String s) {
                LogUtil.d(TAG, "错误码是 -->" + i);
                LogUtil.d(TAG, "错误信息是 -->" + s);
                //发生错误，更新UI
                handlerError();
            }
        });

    }

    /**
     * 更新为正在加载的页面
     */
    private void updateLoading() {
        if (mRecommendViewCallBacks != null) {
            for (IRecommendViewCallBack recommendViewCallBack : mRecommendViewCallBacks) {
                //View逻辑对象调用加载数正在加载页面方法
                recommendViewCallBack.onLoadLoading();
            }
        }
    }



    /**
     * 处理返回的推荐内容结果
     * @param albumList 获得的专辑列表
     */
    private void handlerRecommendResult(List<Album> albumList) {
        if (albumList != null) {
//            //测试，清空一下，让界面显示空
//            albumList.clear();
            if (albumList.size() == 0) {
                if (mRecommendViewCallBacks != null) {
                    for (IRecommendViewCallBack recommendViewCallBack : mRecommendViewCallBacks) {
                        //View逻辑对象调用加载数据为空页面方法
                        recommendViewCallBack.onLoadDataEmpty();
                    }
                }
            } else {
                if (mRecommendViewCallBacks != null) {
                    for (IRecommendViewCallBack recommendViewCallBack : mRecommendViewCallBacks) {
                        //View逻辑对象调用加载推荐专辑列表方法,填入数据，更新UI
                        recommendViewCallBack.onLoadRecommendAlbumList(albumList);
                    }
                    this.mCurrentRecommendList = albumList;
                }
            }
        }
    }

    /**
     * 处理请求失败错误
     */
    private void handlerError() {
        if (mRecommendViewCallBacks != null) {
            for (IRecommendViewCallBack recommendViewCallBack : mRecommendViewCallBacks) {
                //View逻辑对象调用加载网路错误页面方法
                recommendViewCallBack.onLoadNetworkError();
            }
        }

    }

    @Override
    public void registerViewCallBack(IRecommendViewCallBack recommendViewCallBack) {
        //防止重复添加
        if (!mRecommendViewCallBacks.contains(recommendViewCallBack)) {
            mRecommendViewCallBacks.add(recommendViewCallBack);
        }
    }

    @Override
    public void unRegisterViewCallBack(IRecommendViewCallBack recommendViewCallBack) {
        if (mRecommendViewCallBacks != null) {
            mRecommendViewCallBacks.remove(recommendViewCallBack);
        }
    }
}
